package com.xxx.consumer;

import com.alibaba.fastjson.JSONObject;
import com.xxx.domain.MediaFile;
import com.xxx.result.JSONResult;
import com.xxx.service.impl.MediaFileServiceImpl;
import com.xxx.util.AssertUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.MessageModel;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;

/**
 * 指定组名
 * 订阅的主题topic
 * 订阅的Tag
 * 消费模式 默认集群模式
 */

@RocketMQMessageListener(consumerGroup = "video-push-stream-consumer",
        topic = "topic-video",
        selectorExpression = "*",
        messageModel = MessageModel.CLUSTERING)
@Component
@Slf4j
public class VideoPushConsumer implements RocketMQListener<MessageExt> {
    @Autowired
    private MediaFileServiceImpl mediaFileService;

    @Override
    public void onMessage(MessageExt message) {
        byte[] body = message.getBody();
        String messageStr = new String(body, StandardCharsets.UTF_8);
        log.info("接收到MQ消息:{}", messageStr);
        //json字符串转对象
        MediaFile mediaFile = JSONObject.parseObject(messageStr, MediaFile.class);
        //调用推流服务去推流
        JSONResult consumerResult = mediaFileService.handleFile2m3u8(mediaFile);
        AssertUtil.isTrue(consumerResult.isSuccess(), "调用推流方法异常");
    }
}
